package com.jyh.blog.strategy.impl;

import com.alibaba.fastjson2.JSON;
import com.jyh.blog.config.QQConfigProperties;
import com.jyh.blog.dto.QQTokenDTO;
import com.jyh.blog.dto.QQUserInfoDTO;
import com.jyh.blog.dto.SocialTokenDTO;
import com.jyh.blog.dto.SocialUserInfoDTO;
import com.jyh.blog.exception.MyException;
import com.jyh.blog.utils.CommonUtils;
import com.jyh.blog.vo.QQLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.jyh.blog.constant.SocialLoginConst.*;
import static com.jyh.blog.enums.LoginTypeEnum.QQ;
import static com.jyh.blog.utils.HttpCode.QQ_LOGIN_ERROR;

@Service("qqLoginStrategyImpl")
public class QQLoginStrategyImpl extends AbstractSocialLoginStrategyImpl {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private QQConfigProperties qqConfigProperties;

    @Override
    public SocialTokenDTO getSocialToken(String data) {
        QQLoginVO qqLoginVO = JSON.parseObject(data, QQLoginVO.class);
        //校验
        checkQQtoken(qqLoginVO);
        //封装
        return SocialTokenDTO.builder()
                .accessToken(qqLoginVO.getAccessToken())
                .openId(qqLoginVO.getOpenId())
                .loginType(QQ.getType())
                .build();
    }

    @Override
    public SocialUserInfoDTO getSocialUserInfo(SocialTokenDTO socialToken) {
        //请求参数
        Map<String, String> qqMap = new HashMap<>(3);
        qqMap.put(QQ_OPEN_ID, socialToken.getOpenId());
        qqMap.put(ACCESS_TOKEN, socialToken.getAccessToken());
        qqMap.put(OAUTH_CONSUMER_KEY, qqConfigProperties.getAppId());
        //发送请求
        String result = restTemplate.getForObject(qqConfigProperties.getUserInfoUrl(), String.class, qqMap);
        //封装
        QQUserInfoDTO qqUserInfoDTO = JSON.parseObject(result, QQUserInfoDTO.class);
        return SocialUserInfoDTO.builder()
                .nickname(qqUserInfoDTO.getNickname())
                .avatar(qqUserInfoDTO.getFigureurl_qq_1())
                .build();
    }

    /**
     * 检查qq token 信息
     *
     * @param qqLoginVO
     */
    private void checkQQtoken(QQLoginVO qqLoginVO) {
        //封装
        Map<String, String> qqMap = new HashMap<>(1);
        qqMap.put(ACCESS_TOKEN, qqLoginVO.getAccessToken());
        try {
            //url，返回类型，参数
            String result = restTemplate.getForObject(qqConfigProperties.getCheckTokenUrl(), String.class, qqMap);
            //result 不为空，获取 result 括号中的数据，并将数据转换为 QQToken 对象
            QQTokenDTO qqToken = JSON.parseObject(CommonUtils.getBracketsContent(Objects.requireNonNull(result)), QQTokenDTO.class);
            if (!qqLoginVO.getOpenId().equals(qqToken.getOpenid())) {
                throw new MyException(QQ_LOGIN_ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(QQ_LOGIN_ERROR);
        }
    }
}
